package com.tdt.alarm.controller;

import java.util.Calendar;
import java.util.List;

import com.tdt.alarm.model.AlarmDBHelper;
import com.tdt.alarm.model.SetAlarm;
import com.tdt.alarm.model.Utils;

import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class CheckRepeate extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {

		setAlarms(context,intent);
	}

	public static void setAlarms(Context context,Intent intent) {
		AlarmDBHelper alarmDB = new AlarmDBHelper(context);
		List<SetAlarm> list =alarmDB.getAlarms();
		list.toArray();
		SetAlarm alarm =Utils.getAlarmRingFirst(list);
		cancelAlarms(context);
		PendingIntent pIntent = createPendingIntent(context,alarm);
		
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, alarm.getTime().getHour());
		calendar.set(Calendar.MINUTE, alarm.getTime().getMinute());
		calendar.set(Calendar.SECOND, 00);
		
		final int nowDay = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
		final int nowHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
		final int nowMinute = Calendar.getInstance().get(Calendar.MINUTE);
		boolean alarmSet = false;
		
		//First check if it's later in the week
		for (int dayOfWeek = Calendar.SUNDAY; dayOfWeek <= Calendar.SATURDAY; ++dayOfWeek) {
			if (alarm.getRepeatingDay(dayOfWeek - 1) && dayOfWeek >= nowDay &&
					!(dayOfWeek == nowDay && alarm.getTime().getHour() < nowHour) &&
					!(dayOfWeek == nowDay && alarm.getTime().getHour() == nowHour && alarm.getTime().getMinute() <= nowMinute)) {
				calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);

				setAlarm(context, calendar, pIntent);
				alarmSet = true;
				break;
			}
		}

		//Else check if it's earlier in the week
		if (!alarmSet) {
			for (int dayOfWeek = Calendar.SUNDAY; dayOfWeek <= Calendar.SATURDAY; ++dayOfWeek) {
				if (alarm.getRepeatingDay(dayOfWeek - 1) && dayOfWeek <= nowDay && alarm.isRepeatWeekly()) {
					calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
					calendar.add(Calendar.WEEK_OF_YEAR, 1);

					setAlarm(context, calendar, pIntent);
					alarmSet = true;
					break;
				}
			}
			Toast.makeText(context, "Rung rung if !", Toast.LENGTH_LONG).show();
		}
		else 
		{
//			intent = new Intent(context.getApplicationContext(), AlarmAction.class);
//	    	intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//			context.startActivity(intent);
			Toast.makeText(context, "Rung rung else !", Toast.LENGTH_LONG).show();
		}
		
	}

	public static void cancelAlarms(Context context) {
		AlarmDBHelper dbHelper = new AlarmDBHelper(context);

		List<SetAlarm> alarms = dbHelper.getAlarms();

		if (alarms != null) {
			for (SetAlarm alarm : alarms) {
				if (alarm.isCheckActive()) {
					PendingIntent pIntent = createPendingIntent(context, alarm);
					AlarmManager alarmManager = (AlarmManager) context
							.getSystemService(Context.ALARM_SERVICE);
					alarmManager.cancel(pIntent);
				}
			}
		}

	}

	private static PendingIntent createPendingIntent(Context context,
			SetAlarm model) {

		Intent intent = new Intent(context, RepeatService.class);
		intent.putExtra("id", model.getId());
		intent.putExtra("name", model.getLabel());
		intent.putExtra("hour", model.getTime().getHour());
		intent.putExtra("minute", model.getTime().getMinute());
		intent.putExtra("tone", model.getRingtone());

		return PendingIntent.getService(context, (int) model.getId(), intent,
				PendingIntent.FLAG_UPDATE_CURRENT);
	}
	

	@SuppressLint("NewApi")
	private static void setAlarm(Context context, Calendar calendar, PendingIntent pIntent) {
		AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
			alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);
		} else {
			alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);
		}
	}

}
